package com.example.bluetoothapplication;

import android.bluetooth.BluetoothDevice;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.HashSet;

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {


    private ArrayList<BluetoothDevice> bluelist=new ArrayList<>();
    RecyclerAdapter() {}
    //数据传递
    public void setData(HashSet<BluetoothDevice> bluelist){
        if(!this.bluelist.isEmpty()){
            this.bluelist.clear();
        }
        this.bluelist.addAll(bluelist);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.bluetooth_item,parent,false);
        ViewHolder viewHolder=new ViewHolder(view);
        return viewHolder;
    }

    @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.name.setText(bluelist.get(position).getName());
        holder.address.setText(bluelist.get(position).getAddress());
    }

    @Override
    public int getItemCount() {
        if(bluelist==null){
            return 0;
        }else {
        return bluelist.size();
        }
    }
    class ViewHolder extends RecyclerView.ViewHolder{
        TextView name; //蓝牙名字
        TextView address; //蓝牙地址
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            name=itemView.findViewById(R.id.tv_name);
            address=itemView.findViewById(R.id.tv_address);
        }
    }

}
